<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新品有戏商家合作平台</title>
    <script src="__JS__/jquery.js"></script>
    <link href="__CSS__/boot.css" rel="stylesheet">
    <link href="__STATIC__/layui/css/layui.css" rel="stylesheet">
    <script src="__JS__/layer/layer.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <script src="__STATIC__/boot/js/bootstrap-table.js"></script>
    <script src="__STATIC__/boot/js/bootstrap-table-zh-CN.min.js"></script>
    <script src="__STATIC__/boot/js/dropdown.js"></script>
    <link rel="stylesheet" href="__STATIC__/boot/css/bootstrap-table.min.css"/>
</head>
<body>
{include file="base/header" /}
<div style="width: 500px;height: 500px;margin: 0 auto;margin-top: 80px">
    <table class="layui-table" style="text-align: center">
        <caption style="text-align: center;font-size: 30px" >优惠券查询</caption>
        <tbody>
        <tr>
            <td style="font-size: 20px">请输入优惠券</td>
            <td><input type="text" name="name" required id="coupon" style="font-size:20px;" lay-verify="required" placeholder="请输入优惠券" autocomplete="off" class="layui-input"></td>
        </tr>
        <tr style="display: none;text-align: center" id="available">
            <td style="font-size: 20px">是否可用</td>
            <td><input type="text" name="available" style="font-size:20px;border: 0px" required disabled lay-verify="required" class="layui-input"></td>
        </tr>
        <tr style="display: none;text-align: center" id="time">
            <td style="font-size: 20px">领取时间</td>
            <td><input  type="text" name="time" required style="font-size:20px;border: 0px" lay-verify="required" disabled class="layui-input"></td>
        </tr>
        <tr>
            <td colspan="2">
                <button class="layui-btn" id="reset">重置</button>
                <button class="layui-btn layui-btn-normal" id="add">添加</button>
                <button class="layui-btn layui-btn-danger" id="total">统计</button>
            </td>
        </tr>
        </tbody>
    </table>
</div>
</body>
<script>
    //查询优惠券是否可用
    $("#coupon").change(function () {
        var id = this.id;
        if(!$("#"+id).val()){
            layer.msg('请输入优惠券',{icon:5,time:3000});
            return;
        }
        $.ajax({
            url:"{:url('index/coupon/queryCoupons')}",
            data:{
                'coupon':$("#"+id).val()
            },
            type:"post",
            dataType:"json",
            success:function (res) {
                if(res.success){
                    $("input[name=available]").val(res.success);
                    $("#available").css('display','');
                    $("#time").css('display','none');
                }else{
                    var result = eval('(' + res.error + ')');
                    $("input[name=available]").val('不可用');
                    $("input[name=time]").val(result.time);
                    $("#available").css('display','');
                    $("#time").css('display','');
                }
            }
        });
    });
    //重置表单
    $("#reset").click(function () {
        $("#time").css('display','none');
        $("#available").css('display','none');
        $("#coupon").val('');
    });
    //添加优惠券
    $("#add").click(function () {
        if(!$("#coupon").val()){
            layer.msg('请输入优惠券',{icon:5,time:3000});
            return;
        }
        $.ajax({
            url:"{:url('index/coupon/insert')}",
            data:{
                'coupon':$("#coupon").val()
            },
            type:"post",
            dataType:"json",
            success:function (res) {
                if(res.success){
                    layer.msg(res.success,{icon:1,time:2000},function () {
                        window.location.reload();
                    });
                }else{
                    layer.msg(res.error,{icon:5,time:2000},function () {
                        window.location.reload();
                    })
                }
            }
        });
    });
    //统计
    $("#total").click(function () {
        layer.open({
            type:2,
            title:'优惠券统计',
            area:['500px','350px'],
            offset: 'auto',
            content:"{:url('index/coupon/total')}"
        });
    })
</script>
</html>